package com.ibm.mall.action;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ibm.mall.base.BaseAction;
import com.ibm.mall.model.OrderForm;
import com.ibm.mall.model.PageBean;
import com.ibm.mall.model.User;
import com.ibm.mall.util.MD5Util;
import com.ibm.mall.util.QueryHelper;
import com.opensymphony.xwork2.ActionContext;

@Controller
@Scope("prototype")
public class UserAction extends BaseAction<User>{

	private static final long serialVersionUID = 1L;
	
	private User user;
	
	private Map responseJson;
	
	public String registerUI() {
		return "registerUI";
	}
	
	public String register() {
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		User u=userService.getUserByUserAccount(user.getUserAccount());
		if(u==null){
			user.setUserPassword(MD5Util.MD5(user.getUserPassword()));
			user.setUserBalance(new BigDecimal(0));
			user.setInsertTime(sf.format(new Date()));
			user.setStatus((short) 0);
			userService.saveUser(user);
			return "registerSuccess";
		}else{
			ActionContext.getContext().put("msg", "用户名已存在!");
			return "registerUI";
		}
	}
	
	public String loginUI() {
		return "loginUI";
	}
	
	public String login() {
		User u=userService.getUserByUserAccount(user.getUserAccount());
		if(u==null){
			ActionContext.getContext().put("msg", "用户名不存在！");
			return "loginUI";
		}else{
			if(u.getUserPassword().equals(MD5Util.MD5(user.getUserPassword()))){
				ActionContext.getContext().getSession().put("user", u);
				return "toIndex";
			}else{
				ActionContext.getContext().put("msg", "密码不正确！");
				return "loginUI";
			}
		}
	}

	public String logout() {
		ActionContext.getContext().getSession().remove("user");
		ActionContext.getContext().getSession().remove("car");
		ActionContext.getContext().getSession().remove("carSize");
		ActionContext.getContext().getSession().remove("totalPrice");
		return "toIndex";
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	public String list() {
		return "list";
	}
	
	public String getData(){
        Map<String, Object> map = new HashMap<String, Object>();
        QueryHelper queryHelper=new QueryHelper(User.class, "u")//
			.addCondition("u.status=?", (short)0)//查询出未被删除的
			.addOrderProperty("u.id", true);
        //分页查询获得pageBean
        PageBean pageBean=userService.getPageBean(page,rows,queryHelper);
        map.put("rows", pageBean.getRecordList());
        map.put("total", pageBean.getRecordCount());
        this.setResponseJson(map);
        return "getData";
    }
	
	public Map getResponseJson() {   
        return responseJson;   
    }
    
    public void setResponseJson(Map responseJson) {   
        this.responseJson = responseJson;   
    }
}
